# https://www.52pojie.cn/thread-2073915-1-1.html

def data_type_exercises():
    # 练习1：位操作
    byte_value = 0b10100101  # 二进制表示
    bit_0 = (byte_value & 0x01) == 0x01  # 检查第0位
    bit_2 = (byte_value & 0x04) == 0x04  # 检查第2位
    print(f"字节值: {bin(byte_value)}")
    print(f"位0: {bit_0}, 位2: {bit_2}")

    # 练习2：字节序理解
    # 数字 1000 的字节表示
    # 大端序: [0x03, 0xE8] - 西门子PLC常用
    # 小端序: [0xE8, 0x03] - Modbus常用

    big_endian_bytes = bytes([0x03, 0xE8])
    little_endian_bytes = bytes([0xE8, 0x03])

    value_big = int.from_bytes(big_endian_bytes, 'big')  # 1000
    value_little = int.from_bytes(little_endian_bytes, 'little')  # 1000

    return value_big, value_little


# 运行练习
data_type_exercises()